package org.easy.file.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.easy.file.monitor.FileTask;
import org.easy.file.monitor.ITask;
import org.easy.tool.consts.EasyConstants;
import org.easy.tool.string.StrUtil;

import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description:
 * @Author: wxl
 * @CreateTime: 2022/12/28 11:14
 */
@Data
@ApiModel(value = "文件监控实体类",description = "文件监控实体类")
public class FileModel {

    /**
     * 任务ID
     */
    @ApiModelProperty(name = "taskId",value = "任务ID")
    private String taskId;
    /**
     * 文件/文件夹路径
     */
    @ApiModelProperty(name = "filePath",value = "文件/文件夹路径")
    private String filePath;
    /**
     * 是否运行
     */
    @ApiModelProperty(name = "isRunning",value = "是否运行")
    private boolean isRunning;
    /**
     * 是否开启成功
     */
    @ApiModelProperty(name = "isSuccess",value = "是否开启成功：true->成功，false->失败")
    private boolean isSuccess;
    /**
     * 启动时间
     */
    @ApiModelProperty(name = "startTime",value = "启动时间")
    private String startTime;
    /**
     * 停止时间
     */
    @ApiModelProperty(name = "stopTime",value = "停止时间")
    private String stopTime;

    private FileModel () {}

    public FileModel (ITask iTask) {
        this.taskId = iTask.getTaskId();
        this.filePath = iTask.getFilePath();
        this.isRunning = iTask.isRunning();
        this.isSuccess = iTask.isSuccess();
        this.startTime = iTask.getStartTime();
        this.stopTime = iTask.getStopTime();
    }

    @Override
    public String toString () {
        StringJoiner joiner = new StringJoiner("-");
        joiner.add(this.getTaskId());
        joiner.add(this.getFilePath());
        joiner.add(String.valueOf(this.isRunning()));
        joiner.add(StrUtil.isNotEmpty(this.getStartTime()) ? this.getStartTime() : EasyConstants.CONST_TASK_NON);
        joiner.add(String.valueOf(this.isSuccess()));
        joiner.add(StrUtil.isNotEmpty(this.getStopTime()) ? this.getStopTime() : EasyConstants.CONST_TASK_NON);
        return joiner.toString();
    }

    public ITask stringToTask () {
        ITask iTask = new FileTask(this.getFilePath());
        iTask.setTaskId(this.getTaskId());
        iTask.setRunning(this.isRunning());
        iTask.setSuccess(this.isSuccess());
        if (StrUtil.isNotEmpty(this.getStartTime())) {
            iTask.setStartTime(this.getStartTime());
        }
        if (StrUtil.isNotEmpty(this.getStopTime())) {
            iTask.setStopTime(this.getStopTime());
        }
        return iTask;
    }

    public static ITask stringToTask (String str) {
        FileModel fileModel = stringToModel(str);
        return fileModel.stringToTask();
    }

    public  Map<String,String> fileToMap () {
        Map<String,String> map = new ConcurrentHashMap<>(1 << 8);
        map.put(this.getTaskId(),this.toString());
        return map;
    }

    public static FileModel stringToModel(String str){
        String[] array = StrUtil.splitToArray(str,"-");
        FileModel fileModel = new FileModel();
        fileModel.setTaskId(array[0]);
        fileModel.setFilePath(array[1]);
        fileModel.setRunning(Boolean.parseBoolean(array[2]));
        if (!EasyConstants.CONST_TASK_NON.equals(array[3])) {
            fileModel.setStartTime(array[3]);
        }
        fileModel.setSuccess(Boolean.parseBoolean(array[4]));
        if (!EasyConstants.CONST_TASK_NON.equals(array[5])) {
            fileModel.setStopTime(array[5]);
        }
        return fileModel;
    }

}
